// pages/lawyerTool/lawyerTool.js
import postData from '../../../utils/vendor/common/common.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: false,
    isslide_up: false,
    caseListData:{},//案件类型对象
    cityJsonList:"",//城市列表
    targetAmount:"",
    conditionData: {
      caseName: "",
      cityName: ""
    },
    contentDet:"",
    hanziNum:"",
    lawyerFee:"",
    zcjdData:"",
    scjdData:"",
    spjdData:"",
    showClear: false, //输入框删除图标
    lawyerFeeShow:false,
    xsCaseShow:false,
    isCityShow:false,
    lawyerDet:false,
    showBtn: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options=======",options)
    if (options.id == 'share') {
      this.setData({
        showBtn: true
      });
    }
    wx.showShareMenu({
      withShareTicket: true
    })
    let caseListData = {};
    let cityJsonList = postData.toolCityList;
    //存储数据对象
    let tempCaseList = postData.caseListJson;
    for (let i = 0; i < tempCaseList.length; i++) {
      caseListData[tempCaseList[i].name] = tempCaseList[i];
    }
    this.setData({
      caseListData: caseListData,
      cityJsonList: cityJsonList,
      conditionData: {
        caseName: "民事案件",
        cityName: "上海"
      }
    });
  },

  //点击回首页
  goHome: function () {
    wx.redirectTo({
      url: '../../index/index',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.tool = this.selectComponent("#tool");
  },


  //触发输入金额
  targetAmountChange: function (e) {
    // console.log(e.detail.value);
    let value = e.detail.value;
    if (!this.judeNum(value)) {
      this.setData({
        lawyerFeeShow: false,
        hanziNum: "",
        lawyerFee:"",
        targetAmount:"",
        showClear:false,
        lawyerDet: false
      });
    }else{
      let hanziNum = postData.numToHanzi(value);
      let caseListData = this.data.caseListData;
      let caseData = caseListData[this.data.conditionData.cityName];
      let lawyerFee = postData.getlayyerFee(caseData.qjList, caseData.minData, value, caseData.qjFlag, caseData.containFlag);
      this.setData({
        lawyerFeeShow: true,
        hanziNum: hanziNum,
        lawyerFee: lawyerFee,
        targetAmount: value,
        showClear:true
      });
      let _this = this;
      setTimeout(function(){
        _this.setData({
          lawyerDet: true
        });
      },300);
    }
  },


  //点击清空标的
  clearNum: function () {
    this.setData({
      lawyerFeeShow: false,
      hanziNum: "",
      lawyerFee: "",
      targetAmount: "",
      showClear: false,
      lawyerDet: false
    });
  },

  //判断是否位数字
  judeNum: function (value) {
    var patrn = /^(-)?\d+(\.\d+)?$/;
    if (patrn.exec(value) == null || value == "") {
      return false
    } else {
      return true
    }
  },

  // 显示案件类型
  showCaseList: function() {
    this.setData({
      isShow: true
    })
    let _this = this;
    setTimeout(function(){
      _this.setData({
        isslide_up: true
      });
    },300);

  },
  // 取消案件类型
  selectCase: function(e) {
    let flag = e.currentTarget.dataset.key;
    // console.log("eeeeeeeee", flag);
    this.setData({
      isslide_up: false
    })
    let _this = this;
    setTimeout(function () {
      _this.setData({
        isShow: false
      });
    }, 300);
    if (flag == '4'){
      return;
    }
    if (this.data.conditionData.caseName == "刑事案件") {
      if (flag == '1' || flag == '2') {
        this.setData({
          xsCaseShow: false,
          lawyerFeeShow: false,
          targetAmount:""
        });
      }
    } else {
      if (flag == '3') {
        let caseListData = this.data.caseListData;
        let caseData = caseListData[this.data.conditionData.cityName];
        this.setData({
          xsCaseShow:true,
          lawyerFeeShow:true,
          zcjdData: caseData.zcjd,
          scjdData: caseData.scqsjd,
          spjdData: caseData.spjd
        });
      }
    }

    let caseName = "";
    if (flag == '1') {
      caseName = "民事案件";
    } else if (flag == '2') {
      caseName = "行政案件";
    } else if (flag == '3') {
      caseName = "刑事案件";
    }
    let cityName = this.data.conditionData.cityName;
    this.setData({
      conditionData: {
        caseName: caseName,
        cityName: cityName
      }
    });
  },

  //点击显示介绍信息
  showIntroduce: function () {
   let caseListData = this.data.caseListData
   let caseData = caseListData[this.data.conditionData.cityName];
   let contentDet = postData.convertHtmlToText(caseData.content);
   app.globalData.toolContentDet = contentDet;
   app.globalData.toolTitle = this.data.conditionData.cityName + "律师收费标准";
   wx.navigateTo({
     url: '../intro/intro',
   }) 
  },

  //点击显示城市列表
  showCityList: function(){
    this.setData({
      isCityShow: true
    });
    let _this = this;
    setTimeout(function(){
      _this.setData({
        isCityListShow: true
      });
    },300);
  },


  //取消城市列表
  cancleCitylist: function (e) {
    let key = e.currentTarget.dataset.key;
    let city = this.data.cityJsonList[key];
    let caseName = this.data.conditionData.caseName;
    let caseListData = this.data.caseListData;
    var caseData = caseListData[city];
    let zcjdData = this.data.zcjdData, scjdData = this.data.scjdData, spjdData = this.data.spjdData, lawyerFee = this.data.lawyerFee;
    if (caseName == "刑事案件") {
      zcjdData = caseData.zcjd;
      scjdData = caseData.scqsjd;
      spjdData = caseData.spjd;
    } else {
      lawyerFee = postData.getlayyerFee(caseData.qjList, caseData.minData, this.data.targetAmount, caseData.qjFlag, caseData.containFlag);
    }

    this.setData({
      isCityListShow: false,
      conditionData:{
        cityName: city,
        caseName: caseName
      },
      zcjdData: zcjdData,
      scjdData: scjdData,
      spjdData: spjdData,
      lawyerFee: lawyerFee
    });
    let _this = this;
    setTimeout(function () {
      _this.setData({
        isCityShow: false
      });
    }, 300);
   
  },
  
  //点击去诉讼费
  // _goSSF: function () {
  //   if (this.data.showBtn){
  //     wx.navigateTo({
  //       url: '../ssf/ssf',
  //     })
  //   }else{
  //     wx.navigateBack({
  //       delta: 1
  //     })
  //   }
    
  // },

  //点击去律师费
  // _goLSF: function () {
    // wx.navigateTo({
    //   url: '../lsf/lsf',
    // })
  // },

  //点击债权计算
  // _goZQJS: function () {
  //   wx.navigateTo({
  //     url: '../zqjs/zqjs',
  //   })
  // },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log("来自页面内的转发", res.target)
    }
    return {
      title: '律师费计算工具',
      path: 'pages/lawyerTool/lsf/lsf?id=share',
      // imageUrl: picUrl,
      success: function (res) {
        // 转发成功
        console.log("转发成功返回数据=", res)
        wx.showShareMenu({
          // 要求小程序返回分享目标信息
          withShareTicket: true
        });
      },
      fail: function (res) {
        // 转发失败
      },
      complete: function () {

      }
    }
  }
})